package com.zjsru.oneday202208;

/**
 * @Author: likew
 * @Date: 2022/8/14
 * 分割字符串的最大得分:「分割字符串的得分」为 左 子字符串中 0 的数量加上 右 子字符串中 1 的数量。
 * 输入：s = "011101"
 * 输出：5
 * 解释：
 * 将字符串 s 划分为两个非空子字符串的可行方案有：
 * 左子字符串 = "0" 且 右子字符串 = "11101"，得分 = 1 + 4 = 5
 * 左子字符串 = "01" 且 右子字符串 = "1101"，得分 = 1 + 3 = 4
 * 左子字符串 = "011" 且 右子字符串 = "101"，得分 = 1 + 2 = 3
 * 左子字符串 = "0111" 且 右子字符串 = "01"，得分 = 1 + 1 = 2
 * 左子字符串 = "01110" 且 右子字符串 = "1"，得分 = 2 + 1 = 3
 *
 */
public class MaxScore {
    /**
     * 暴力模拟
    * */
    public int maxScore(String s) {
        char[] chars = s.toCharArray();
        int ans = -1;
        int leftScore = 0, rightScore = 0,left = 0, right = chars.length;
        StringBuilder leftSb =  new StringBuilder();
        while(left < right){
            StringBuilder rightSb = new StringBuilder();
            leftSb.append(chars[left]);
            for (int i = left + 1; i < right; i++) {
                rightSb.append(chars[i]);
            }
            if(leftSb.toString().isEmpty() || rightSb.toString().isEmpty()){
                break;
            }
            leftScore = culLeftSorce(leftSb);
            rightScore = culRightSorce(rightSb);
            ans = Math.max(ans, leftScore + rightScore);
            left++;
        }
        return ans;
    }
    
    private int culRightSorce(StringBuilder rightSb) {
        int ans = 0;
        char[] chars = rightSb.toString().toCharArray();
        for (char c:chars) {
            if(c == '1'){
                ans ++;
            }
        }
        return ans;
    }
    
    private int culLeftSorce(StringBuilder leftSb) {
        int ans = 0;
        char[] chars = leftSb.toString().toCharArray();
        for (char c:chars) {
            if(c == '0'){
                ans ++;
            }
        }
        return ans;
    }
    
    /**
     * 模拟方式2
     */
    
    public int maxScore1(String s) {
        int ans = -1;
        int len = s.length(), cur = s.charAt(0) == '0' ? 1: 0;
        for (int i = 1; i < len; i++) {
            cur += s.charAt(i) - '0';
        }
        ans = cur;
        for (int i = 1; i < len - 1; i++) {
            cur += s.charAt(i) == '0' ? 1 : -1;
            ans = Math.max(ans,cur);
        }
        return ans;
    }
    
    public static void main(String[] args) {
        MaxScore maxScore = new MaxScore();
        String s = "10011";
        System.out.println(maxScore.maxScore1(s));
    }
}
